package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * NPT015投核保校验
 *
 * @author : 追到乌云的尽头找太阳-（Jacob）
 * @date : 2020/03/19 15:32
 **/
public class Npt015InsuranceStrategy implements InsuranceStrategy {

    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                    ProductModel productModel, ProductConfig productConfig) {
        // 职业类别，product配置文件不适合NPT015
        String[] occupationTypeList = {"1", "2", "3", "4", "5", "6"};
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        //年金领取频率不能为空
        if (StringUtils.isBlank(requestModel.getProductInfo().getGetDutyKind())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_BLANK_GETDUTYKIND_MESSAGE);
        }
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        AppntModel appntModel = requestModel.getCustInfo().getAppnt();

        Date cValiDate = requestModel.getPolicyInfo().getCvalidate();
        //被保人信息
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                
                String type = insurantModel.getOccupationType();
                //1.校验职业类别是否填写
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
                int insurantYear = DateUtil.getYears(insurantModel.getBirthday(), cValiDate);
                // 被保人是未成年人，投保人必须是其父母
                if (insurantYear < UnderWriteConstant.AGE_EIGHTEEN_YEAR && !AcceptConstant.PARENTAL_RELATIONSHIP.contains(insurantModel.getRelToApp())) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0041_MESSAGE);
                }

                // 被保险人是成年人的，投被关系必须是本人、父母、配偶、子女
                if (insurantYear >= UnderWriteConstant.AGE_EIGHTEEN_YEAR && !(productConfig.getRelationList().contains(insurantModel.getRelToApp()))) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
                }

                boolean flag = false;
                for (String occupation : occupationTypeList) {
                    if (type.equals(occupation)) {
                        flag = true;
                        break;
                    }
                }
                //2.NPT015的险种，职业类别不能为7类
                if (!flag) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE, type));
                }
                int appantYears = DateUtil.getYears(requestModel.getCustInfo().getAppnt().getAppntBirthday(), cValiDate);
                if (appantYears <= UnderWriteConstant.AGE_SIXTY_YEAR && !this.isSamePerson(requestModel.getCustInfo().getAppnt(), insurantModel)
                        && !UnderWriteConstant.SINGLE_PAY.equals(requestModel.getProductInfo().getPayIntv())) {
                    // 如果投保人年龄小于等于60岁且投保人和被保人非同一人且交费方式不是趸交的，投保人既往黑名单、既往理赔等核保通用规则
                    //2.黑名单
                    if (appntModel.getBlackListFlag() > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0032,
                                String.format(UnderWriteConstant.UNDER_WRITE_NUGG0032_MESSAGE, appntModel.getAppntName()));
                    }
                    //3.理赔标识
                    if (appntModel.getClaimFlag() > 0) {
                        return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0033,
                                String.format(UnderWriteConstant.UNDER_WRITE_NUGG0033_MESSAGE, appntModel.getAppntName()));
                    }
                }
                break;
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }

    //判断是否为同一人
    private boolean isSamePerson(AppntModel appntModel, InsurantModel insurantModel) {
        String appBirthday = DateUtil.format(appntModel.getAppntBirthday(), "yyyy-MM-dd");
        String insurantBirthday = DateUtil.format(insurantModel.getBirthday(), "yyyy-MM-dd");
        return (StringUtils.equals(appntModel.getAppntName(), insurantModel.getName())
                && StringUtils.equals(appntModel.getAppntSex(), insurantModel.getSex())
                && StringUtils.equals(appBirthday, insurantBirthday)
                && StringUtils.equals(appntModel.getIdNo(), insurantModel.getIdNo())
                && StringUtils.equals(appntModel.getIdType(), insurantModel.getIdType()));
    }

}
